package com.elong.qq.http;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * QQHttpRequest
  * @Title: QQHttpRequest
  * @Desription qq 请求
  * @author Guanguo.Gao
  * @date 2015年1月12日 下午8:17:44
 */
public class QQHttpRequest {
    
    /**
     * url
     */
    private String url;
    
    /*
     * 超时时间
     */
    private String method;
    
    /**
     * 超时时间
     */
    private int timeOut;
    
    /**
     * 请求的头部
     */
    private Map<String, String> headerMap;
    
    /**
     * 请求的值集合
     */
    private Map<String, String> postMap;
    
    /**
     * post的文件列表
     */
    private Map<String, File> fileMap;
    
    /**
     * get方式的值集合
     */
    private Map<String, String> getMap;
    
    /**
     * 请求的数据流
     */
    private InputStream inputStream;

    /**
     * 保存的输出流
     */
    private OutputStream outputStream;
    
    /**
     * 编码
     */
    private String charset;
    
    /**
     * 连接超时
     */
    private int connectTimeout;
    
    /**
     * 读取超时
     */
    private int readTimeout;
    
    public QQHttpRequest(String url, String method){
	this.url = url;
	this.method = method;
	this.headerMap = new HashMap<String, String>();
	this.postMap = new HashMap<>();
	this.getMap = new HashMap<String, String>();
	this.fileMap = new HashMap<String, File>();
    }


    /**
     * setter method for url
     * @Description the url to set
     * @param url 
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * getter method for method
     * @return the method
     */
    public String getMethod() {
        return method;
    }

    /**
     * setter method for method
     * @Description the method to set
     * @param method 
     */
    public void setMethod(String method) {
        this.method = method;
    }

    /**
     * getter method for timeOut
     * @return the timeOut
     */
    public int getTimeOut() {
        return timeOut;
    }

    /**
     * setter method for timeOut
     * @Description the timeOut to set
     * @param timeOut 
     */
    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }

    /**
     * getter method for headerMap
     * @return the headerMap
     */
    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    /**
     * setter method for headerMap
     * @Description the headerMap to set
     * @param headerMap 
     */
    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    /**
     * getter method for postMap
     * @return the postMap
     */
    public Map<String, String> getPostMap() {
        return postMap;
    }

    /**
     * setter method for postMap
     * @Description the postMap to set
     * @param postMap 
     */
    public void setPostMap(Map<String, String> postMap) {
        this.postMap = postMap;
    }

    /**
     * getter method for fileMap
     * @return the fileMap
     */
    public Map<String, File> getFileMap() {
        return fileMap;
    }

    /**
     * setter method for fileMap
     * @Description the fileMap to set
     * @param fileMap 
     */
    public void setFileMap(Map<String, File> fileMap) {
        this.fileMap = fileMap;
    }

    /**
     * getter method for getMap
     * @return the getMap
     */
    public Map<String, String> getGetMap() {
        return getMap;
    }

    /**
     * setter method for getMap
     * @Description the getMap to set
     * @param getMap 
     */
    public void setGetMap(Map<String, String> getMap) {
        this.getMap = getMap;
    }

    /**
     * setter method for inputStream
     * @Description the inputStream to set
     * @param inputStream 
     */
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * getter method for outputStream
     * @return the outputStream
     */
    public OutputStream getOutputStream() {
        return outputStream;
    }

    /**
     * setter method for outputStream
     * @Description the outputStream to set
     * @param outputStream 
     */
    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * getter method for charset
     * @return the charset
     */
    public String getCharset() {
        return charset;
    }

    /**
     * setter method for charset
     * @Description the charset to set
     * @param charset 
     */
    public void setCharset(String charset) {
        this.charset = charset;
    }

    /**
     * getter method for connectTimeout
     * @return the connectTimeout
     */
    public int getConnectTimeout() {
        return connectTimeout;
    }

    /**
     * setter method for connectTimeout
     * @Description the connectTimeout to set
     * @param connectTimeout 
     */
    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    /**
     * getter method for readTimeout
     * @return the readTimeout
     */
    public int getReadTimeout() {
        return readTimeout;
    }

    /**
     * setter method for readTimeout
     * @Description the readTimeout to set
     * @param readTimeout 
     */
    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }
    
    /**
     * setBody
      * @Title: setBody
      * @Description: TODO
      * @param keymap
     */
    public void setBody(Map<String, String> keymap) {
    	this.postMap = keymap;
    }
    
    /**
     * 添加POST的值
     *
     * @param key a {@link java.lang.String} object.
     * @param value a {@link java.lang.String} object.
     */
    public void addPostValue(String key, String value) {
    	this.postMap.put(key, value);
    }
    
    
    /**
     * addPostFile
      * @Title: addPostFile
      * @Description: TODO
      * @param key
      * @param file
     */
    public void addPostFile(String key, File file){
	this.fileMap.put(key, file);
    }
    
    public void addGetValue(String key, String value){
	this.getMap.put(key, value);
    }
    
    
    public void setBody(InputStream inputStream){
	this.inputStream = inputStream;
    }
    
    /**
     * 添加请求头
     *
     * @param key a {@link java.lang.String} object.
     * @param value a {@link java.lang.String} object.
     */
    public void addHeader(String key, String value)
    {
    	this.headerMap.put(key, value);
    }
    
    
    /**
     * <p>Getter for the field <code>inputStream</code>.</p>
     *
     * @return the inputStream
     */
    public InputStream getInputStream()
    {
    	if(this.inputStream!=null) {
    		return this.inputStream;
    	}else if(this.postMap.size()>0) {
    		addHeader("Content-Type", "application/x-www-form-urlencoded");
    		StringBuffer buffer = new StringBuffer();
        	Iterator<String> it = this.postMap.keySet().iterator();
        	String charset = "utf8";
        	while(it.hasNext()) {
        		String key = it.next();
        		String value = this.postMap.get(key);
        		try {
        		    key = URLEncoder.encode(key, charset);
        		    value = URLEncoder.encode(value==null?"":value, charset);
    	           	    buffer.append(key);
    	           	    buffer.append("=");
    	                    buffer.append(value);
    	                    buffer.append("&");
        		} catch (Exception e) {
        		    throw new RuntimeException(e);
        		}
        	}
        	try {
	            return new ByteArrayInputStream(buffer.toString().getBytes(charset));
            } catch (UnsupportedEncodingException e) {
            	throw new RuntimeException(e);
            }
    	}else {
    		return null;
    	}
    }
    
    
    /**
     * <p>Getter for the field <code>url</code>.</p>
     *
     * @return the url
     */
    public String getUrl()
    {
    	if(this.getMap.size()>0){
    		StringBuffer buffer = new StringBuffer(url);
    		buffer.append("?");
    		Iterator<String> it = this.getMap.keySet().iterator();
        	String charset = "utf8";
        	while(it.hasNext()) {
        		String key = it.next();
        		String value = this.getMap.get(key);
        		try {
        		    key = URLEncoder.encode(key, charset);
        		    value = URLEncoder.encode(value==null?"":value, charset);
        		    buffer.append(key);
        		    buffer.append("=");
        		    buffer.append(value);
        		    if(it.hasNext())	  
    	            		buffer.append("&");
        		} catch (Exception e) {
        		    throw new RuntimeException(e);
        		}
        	}
    			return buffer.toString();
    	}else{
    		return url;
    	}
    }
    
    public static void main(String[] args) {
	QQHttpRequest request = new QQHttpRequest("http://www.baidu.com", null);
	request.addGetValue("nihao", "test");
	request.addGetValue("hello", "world");
	System.out.println(request.getUrl());
    }
    
}
